package com.fairytask.ui
{
	import com.fairytask.data.BaseData;
	import com.fairytask.data.RecordObject;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.RecordDefine;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.ActiveGiftItem;
	import com.fairytask.ui.item.activeItem.ActiveTable;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	/**
	 * 活跃度ui 
	 * @author xujh
	 * 
	 */
	public class ActiveDegreeUI extends FrameUI
	{
		private static var _self		:ActiveDegreeUI;
		//显示
		public var giftBG		:MovieClip;
		public var tableBg		:MovieClip;
		public var giftViewGrop	:ViewGroup;	//活跃度礼包
		public var giftLab		:Label;		//礼包奖励标题
		public var giftInfo		:Label;		//0点清空
		public var activeNum	:Label;		//活跃度
		public var activeBar	:MovieClip;	//活跃进度条
		
		private var _activeTable	:ActiveTable;	//活跃表
		private var _tableTitle		:Array	=	[1000000646,1000000647,1000000648,1000000649];	//表头
		private var _bindPrizeArr	:Array	=	[];	//绑定奖励表
		private var _bindProp		:Array	=	[];	//绑定数据
		
		private const GIFT_NUM		:int	=	4;
		private var _canGetGift		:Boolean	=	false;
		
		private const BUTTON_OPENUI		:int	=	1;	//打开ui
		private const BUTTON_GOTOWILD	:int	=	2;	//野外打怪
		private const BUTTON_CHAT		:int	=	3;	//聊天栏说话
		
		
		public static function getInstance():ActiveDegreeUI
		{
			if (_self == null)
			{
				_self=new ActiveDegreeUI;
			}
			return _self;
		}
		
		public function ActiveDegreeUI(popUI:Boolean=true)
		{
			super(popUI);
		}
		
		/**
		 * 初始化界面 
		 * 
		 */
		override public function initGUI():void
		{
			//活跃度表格初始化
			for(var i:int=0;i<_tableTitle.length;i++)
			{
				_tableTitle[i]	=	StaticIni.getSwfLangStr(String(_tableTitle[i]));
			}
			_activeTable	=	new ActiveTable(320,360,_tableTitle,null,[110,70,70,50]);
			_activeTable.x	=	465;
			_activeTable.y	=	20;
			windowBG.addChild(_activeTable);
			initGift();
			initEvent();
			initBind();
		}
		
		private function initGift():void
		{
			var giftList	:Array	=	StaticIni.getIniList("GiftRecList");
			for(var i:int=0;i<giftList.length;i++)
			{
				var giftItem	:ActiveGiftItem	=	new ActiveGiftItem();
				giftItem.prizeID	=	giftList[i];
				giftViewGrop.getItemByIndex(i).addItem(giftItem);
			}
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void
		{
			
		}
		
		/**
		 * 绑定数据 
		 * 
		 */
		private function initBind():void
		{
			//活跃度
			//bindManager.BindProp(Player.instance.data, "ActValue",this,"actValue");
			//活跃度奖励
			//bindManager.BindRecord(model.scene,RecordDefine.ACTIVE_PRIZE_REC,onAddPrize,onRemovePrize);
		}
		
		/**
		 * 绑定活跃度表 
		 * 
		 */
		private function initDegreeRec():void
		{
			//先解绑数据
			unBindData();
			//活跃度规则id
			var degreeArr	:Array	=	StaticIni.getIniList("GiftTaskList");
			_activeTable.clear();
			for(var i:int=0;i<degreeArr.length;i++)
			{
				var actId:String = degreeArr[i];	//规则id
				var rowDate:Array = [];
				if(StaticIni.getIniObj(actId)==null)
				{
					continue;
				}
				//活跃度名称
				rowDate.push(StaticIni.getSwfLangStr(StaticIni.getIniVar(actId,"Name")));
				//活跃度id
				var taskID	:int	=	int(StaticIni.getIniVar(actId,"TaskType"));
				//服务端表格为空时，返回
				if(null == model.scene.GetRecordEx(RecordDefine.ACTIVEDEGREEREC))
				{
					return;
				}
				var recObj	:RecordObject	=	model.scene.GetRecordEx(RecordDefine.ACTIVEDEGREEREC).GetPropertiesEx(String(taskID-1));
				//当前完成活动次数
				var curCount:int	=	recObj.GetProperties("2");
				//是否已完成
				var finish	:int	=	recObj.GetProperties("3");
				rowDate.push(curCount + "/" + StaticIni.getIniVar(actId,"TaskCount"));
				rowDate.push(StaticIni.getIniVar(actId,"Value"));
				//活动描述
				var baseObj	:BaseData = new BaseData();
				var describe:String = StaticIni.getSwfLangStr(StaticIni.getIniVar(actId,"DescribeInfo"));
				baseObj.HandleProperties("Describe",describe);
				//已完成
				if(finish == 1)
				{
					//完成
					rowDate.push(StaticIni.getSwfLangStr("1000000653"));
					_activeTable.addRow(rowDate,finish,baseObj);
					//完成图片
//					var clip	:Clip	=	new Clip();
//					clip.source	=	ResConfig.pngResPath	+	"Task_Completed.png";
//					_activeTable.addRow(rowDate,finish,baseObj,clip);
				}
				else
				{
					//添加按钮
					var targetBtn	:Button	=	new Button(StaticIni.getSwfLangStr("1000000651"),50,25,"button_042");
					var btnData	:Object	=	{"ButtonType":StaticIni.getIniVar(actId,"ButtonType"),"UIDefine":StaticIni.getIniVar(actId,"UIDefine"),"FunDefine":StaticIni.getIniVar(actId,"FunDefine")};
					targetBtn.data	=	btnData;
					_activeTable.addRow(rowDate,finish,baseObj,targetBtn,onTargetClick);
					_bindProp.push([recObj,"3",bindManager.BindRecordProp(recObj,"3",changeFinsh)]);
				}
			}
//			_activeTable.addItemEvent(TableEvent.ROW_SELECT,onRowClick);
		}
		
		/**
		 * 解绑数据 
		 * 
		 */
		private function unBindData():void
		{
			var len	:int	=	_bindProp.length;
			for(var i:int=0;i<len;i++)
			{
				bindManager.UnbindProp(_bindProp[i].shift(),_bindProp[i].shift(),_bindProp[i].shift());
			}
			_bindProp	=	[];
		}
		
		/**
		 * 改变完成状态 
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */
		private function changeFinsh(key:String,val:int):void
		{
			if(val == 1)
			{
				//重新更新表格
				initDegreeRec();
			}
		}
		
		/**
		 * 点击活跃度开始按钮 
		 * 
		 */
		private function onTargetClick(evt:MouseEvent):void
		{
			var data	:Object	=	evt.currentTarget.data;
			var type	:int	=	data["ButtonType"];
			if(type == BUTTON_OPENUI)
			{
				//model.dispatchEvent(new OpenUIEvent(OpenUIEvent.OPENUI,data["UIDefine"],data["FunDefine"]));
			}
		}
		
		/**
		 * 添加活跃奖励
		 * 
		 */
		private function onAddPrize(key:String, val:RecordObject):void
		{
			var tmpItem	:ActiveGiftItem	=	giftViewGrop.getItemByIndex(int(key)).getItem() as ActiveGiftItem;
			tmpItem.prizeID	=	int(val.GetProperties("0"));
			_bindPrizeArr[int(key)]	=	[];
			//领取状态
			_bindPrizeArr[int(key)].push([val,"1",bindManager.BindRecordProp(val,"1",canGet,[int(key)])]);
			_bindPrizeArr[int(key)].push([val,"1",bindManager.BindRecordProp(val,"2",isGet,[int(key)])]);
		}
		
		/**
		 * 是否领取 
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */
		private function canGet(key:String,val:int,pList:Array):void
		{
			var index	:int	=	pList[0];
			var tmpItem	:ActiveGiftItem	=	giftViewGrop.getItemByIndex(index).getItem() as ActiveGiftItem;
			tmpItem.canGet	=	Boolean(val);
			checkGift();
		}
		
		/**
		 * 已领取状态 
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */
		private function isGet(key:String,val:int,pList:Array):void
		{
			var index	:int	=	pList[0];
			var tmpItem	:ActiveGiftItem	=	giftViewGrop.getItemByIndex(index).getItem() as ActiveGiftItem;
			tmpItem.isGet	=	Boolean(val);
			checkGift();
		}
		
		/**
		 * 检查按钮状态 
		 * 
		 */
		private function checkGift():void
		{
			_canGetGift	=	false;
			for(var i:int=0;i<GIFT_NUM;i++)
			{
				if((giftViewGrop.getItemByIndex(i).getItem() as ActiveGiftItem).isFlash)
				{
					_canGetGift	=	true;
					//发光
					//model.dispatchEvent(new ActiveBtnEvent(ActiveBtnEvent.BTN_FLASH));
					return;
				}
			}
			//不发光
			//model.dispatchEvent(new ActiveBtnEvent(ActiveBtnEvent.BTN_FLASH_STOP));
		}
		
		/**
		 * 删除活跃奖励
		 * 
		 */
		private function onRemovePrize(key:String, val:RecordObject):void
		{
			
		}
		
		/**
		 * 活跃度数值 
		 * @param val
		 * 
		 */
		public function set actValue(val:int):void
		{
			activeNum.htmlStr	=	"活跃度：" + val;
			//设置进度条
			
		}
		
		/**
		 * 有可以领取的 
		 * @param val
		 * 
		 */
		public function set canGetGift(val:Boolean):void
		{
			_canGetGift	=	val;
		}
		
		override protected function BeforeShow():void
		{
			//活跃度表
			initDegreeRec();
		}
	}
}